package com.lemner.ke.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemner.ke.R;
import com.lemner.ke.activity.DetailActivity;
import com.lemner.ke.activity.PlayActivity;
import com.lemner.ke.base.BaseAdapter;
import com.lemner.ke.base.URL;
import com.lemner.ke.bean.RecommendBean;
import com.lemner.ke.bean.VideoTabRvBean;
import com.lemner.ke.util.LoadImageUtils;

/**

* 作者：张恺

* 时间：2018/10/14 

* 类描述：收藏adapter

*/

public class MyCollectionAdapter extends BaseAdapter<RecommendBean> {

    public MyCollectionAdapter(Context context) {
        super(context);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_pic;
        private TextView tv_type;
        private TextView tv_title;
        private TextView tv_tag1;
        private TextView tv_tag2;
        private View v;
        public MyViewHolder(View itemView) {
            super(itemView);
            v=itemView;
            iv_pic = itemView.findViewById(R.id.iv_collection_pic);
            tv_type = itemView.findViewById(R.id.tv_collection_type);
            tv_title = itemView.findViewById(R.id.tv_collection_title);
            tv_tag1 = itemView.findViewById(R.id.tv_collection_tag1);
            tv_tag2 = itemView.findViewById(R.id.tv_collection_tag2);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.item_my_collection, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyViewHolder viewholder = (MyViewHolder) holder;
        final RecommendBean bean=list.get(position);
        if (bean != null) {
            if (bean.getCoverspath() != null) {
                String url = URL.BASE_URL + bean.getCoverspath();
                LoadImageUtils.loadImage(url, viewholder.iv_pic);
            }
            if (bean.getType() != null) {
                viewholder.tv_type.setText(bean.getType());

            }
            if (bean.getTitle() != null) {
                viewholder.tv_title.setText(bean.getTitle());
            }
            String tags = bean.getTags();
            if (tags != null) {
                String[] tagArray = tags.split("\\,");
                if (tagArray != null && tagArray.length > 0) {
                    for (int i = 0; i < tagArray.length; i++) {
                        if (i == 0) {
                            viewholder.tv_tag1.setText(tagArray[i]);
                            viewholder.tv_tag1.setVisibility(View.VISIBLE);
                        } else if (i == 1) {
                            viewholder.tv_tag2.setText(tagArray[i]);
                            viewholder.tv_tag1.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }
            viewholder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != bean.getEntitytype()) {
                        if ("rich".equals(bean.getEntitytype()) || "cartoon".equals(bean.getEntitytype())) {
                            Intent intent=new Intent(context,DetailActivity.class);
                            Bundle bundle=new Bundle();
                            bean.setIds(bean.getEntityids());
                            bundle.putSerializable("recommend",bean);
                            intent.putExtras(bundle);
                            context.startActivity(intent);
                        } else {
                            Intent passIntent = new Intent(context, PlayActivity.class);
                            VideoTabRvBean videoTabRvBean = new VideoTabRvBean();
                            videoTabRvBean.setIds(bean.getEntityids());
                            videoTabRvBean.setTitle(bean.getTitle());
                            bean.setIds(bean.getEntityids());
                            passIntent.putExtra("bean", videoTabRvBean);
                            context.startActivity(passIntent);
                        }
                    }
                }
            });
        }
    }

}
